package com.zhaojonhy.zhaopangwb.utils;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.ContentValues;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.widget.Toast;

import com.zhaojonhy.zhaopangwb.adapter.ImageBrowserAdapter;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * Created by Administrator on 2016/9/27.
 */

public class ImageUtils {

    private static final String TAG = ImageUtils.class.getSimpleName() ;

    public static final int RQUEST_CODE_FROM_CAMERA = 5001 ;
    public static final int RQUEST_CODE_FROM_ALBUM = 5002 ;
    /*
    * 存放图片的Uri地址
    */
    public static Uri imageUriFromCamera ;
    /*
    * 显示获取照片的不同方式对话框
    */
    public static void showImagePickDialog(final Activity activity) {
        String title = "选择获取照片方式" ;
        String[] items = new String[] {"拍照","相册"} ;
        new AlertDialog.Builder(activity)
                .setTitle(title)
                .setItems(items, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        switch (which){
                            case 0 :
                                pickImageFromCamera(activity) ;
                                break;

                            case 1 :
                                pickImageFromAlbum(activity) ;
                                break;
                        }
                    }
                })
                .show() ;
    }
    /*
   * 打开相机拍照获取图片
   */
    public static void pickImageFromCamera(final Activity activity) {

        imageUriFromCamera =  createImageUri(activity) ;

        Intent intent = new Intent() ;
        // 返回图片在onActivityResult中通过以下代码获取
        // MediaStore.EXTRA_OUTPUT参数不设置时,系统会自动生成一个uri,但是只会返回一个缩略图(Bitmap)
        // Bitmap bitmap = (Bitmap) intent.getExtras().get("data");
        intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE) ; //动作是拍照抓取
        intent.putExtra(MediaStore.EXTRA_OUTPUT,imageUriFromCamera) ; //指定图片存在的Uri的地址上
        activity.startActivityForResult(intent, RQUEST_CODE_FROM_CAMERA);
    }

     /*
   * 打开本地相册获取照片
   */

    //隐式意图
    public static void pickImageFromAlbum(final Activity activity) {
        Intent intent = new Intent() ;
        intent.setAction(Intent.ACTION_GET_CONTENT) ; //获取某一个特定类型的动作
        intent.setType("image/*") ;//匹配图片，如jpg或者png
        activity.startActivityForResult(intent,RQUEST_CODE_FROM_ALBUM);
    }
    /*
    * 打开本地相册获取照片2
    */

    //隐式意图
    public static void pickImageFromAlbum2(final Activity activity) {
        Intent intent = new Intent() ;
        intent.setAction(Intent.ACTION_PICK) ; //选取路径
        intent.setData(MediaStore.Images.Media.EXTERNAL_CONTENT_URI) ;
        activity.startActivityForResult(intent,RQUEST_CODE_FROM_ALBUM);
    }

    /**
     * 创建一条图片Uri，用来存放拍照的图片
     */

    private static Uri createImageUri(Context content) {
        String name = "zpWBImg"+System.currentTimeMillis() ;
        ContentValues values = new ContentValues() ;
        values.put(MediaStore.Images.Media.TITLE,name) ; //图片名
        values.put(MediaStore.Images.Media.DISPLAY_NAME,name +".jpeg") ; //文件名称
        values.put(MediaStore.Images.Media.MIME_TYPE,"image/jpeg") ; //图片类型
        //图片地址
        Uri uri = content.getContentResolver()
                .insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values) ; //内容解析者，理解就是SQL数据库
        return uri ;
    }


    /**
     * 删除一条图片
     */
    public static void deletImageUri( Context context , Uri uri ) {
        context.getContentResolver().delete(imageUriFromCamera,null,null) ;
    }

    /**
     * 获取图片路径
     */
    public static String getImageAbsolutePath( Context context , Uri uri ){
        Cursor cursor = MediaStore.Images.Media.query(context.getContentResolver(),uri,
                new String[]{MediaStore.Images.Media.DATA}) ;
        if(cursor.moveToNext()){ //单图没有必要用while循环
            return cursor.getString(0) ;
        }
        return null ;
    }
    /**
     *  保存图片
     */
    public static void saveFile( Context context , Bitmap bitmap , String fileName ) throws IOException
            ,FileNotFoundException{

        Log.d(TAG,"saveFile-->" + "进来了" ) ;
        // 未安装SD卡时不做保存
        // SD卡的状态
        String storageState = Environment.getExternalStorageState() ;
        //做判断，如果不在挂载的状态直接返回
        Log.d(TAG,"panduan:"+ !storageState.equals(Environment.MEDIA_MOUNTED) ) ;

        if( !storageState.equals(Environment.MEDIA_MOUNTED) ){
            ToastUtils.showToast(context,"未检测到有SD卡",Toast.LENGTH_LONG);
            return ;
        }

        // 图片文件保存路径
        File storageDirectory = Environment.getExternalStorageDirectory() ;
        File path = new File(storageDirectory,"/zhaopWB/weiboimg") ;
        //路径不存在就创建
        if( !path.exists() ){
            path.mkdirs() ;
        }
        //图片文件不存在就创建
        File file = new File(path,fileName) ;
        if( !file.exists() ){
            file.createNewFile() ;
        }
        //将文件压缩进对应的流里
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file)) ;
        bitmap.compress(Bitmap.CompressFormat.JPEG,100,bos) ;//1.格式2.质量3.流
        bos.flush();
        bos.close();
    }

}
